//
//  MineHeaderView.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/5/29.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class MineHeaderView: UIView {
    
    //MARK:- 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "我的"
        label.font = DIMENSION_FONT_OF_SIZE(17)
        label.textColor = hexColor(0x333333,0xBBBBBB)
        addSubview(label)
        return label
    }()
    
    //MARK:- 问题
    lazy var problemBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(imageNamed("problem"), for: .normal)
        btn.addTarget(self, action: #selector(tabClick(_ : )), for: .touchUpInside)
        addSubview(btn)
        return btn
    }()
    
    
    //MARK:-头像
    lazy var headerImageView: UIImageView = {
        let iconView = UIImageView()
        iconView.image = imageNamed("DefaultHeader")
        iconView.layer.cornerRadius = DIMENSION_VALUE(32)
        iconView.layer.masksToBounds = true
        addSubview(iconView)
        return iconView
    }()
    
    //MARK:-昵称
    lazy var nikeLabel: UILabel = {
        let label = UILabel()
        label.text = "游客"
        label.font = DIMENSION_BoldFONT_OF_SIZE(20)
        label.textColor = hexColor(0x333333,0xBBBBBB)
        addSubview(label)
        return label
    }()
    
    //MARK:-ID
    lazy var IDLabel: UILabel = {
        let label = UILabel()
        label.text = "登录/注册"
        label.font = DIMENSION_FONT_OF_SIZE(12)
        label.textColor = hexColor(0x333333,0xBBBBBB)
        addSubview(label)
        return label
    }()
    
    //MARK:-等级标识
    lazy var levelView: UIImageView = {
        let iconView = UIImageView()
        iconView.image = imageNamed("fangwei")
        addSubview(iconView)
        return iconView
    }()
    
    //MARK:- 横向分割线
    lazy var lineView1: UIView = {
        
        let view = UIView()
        view.backgroundColor = hexColor(0xE7E7E7,0xE7E7E7)
        addSubview(view)
        return view
        
    }()
    
    //MARK:- 纵向分割线
    lazy var lineView2: UIView = {
        
        let view = UIView()
        view.backgroundColor = hexColor(0xE1E1E1,0xFEFEFE)
        addSubview(view)
        return view
        
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = HexRGB(0xffffff)
        let tap  = UITapGestureRecognizer.init(target: self, action: #selector(tabClick(_ : )))
        
        self.isUserInteractionEnabled = true
        
        self.addGestureRecognizer(tap)
        
        initSubViews()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func initSubViews() {
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(kStatusBarheight())
            make.width.height.equalTo(DIMENSION_VALUE(44))
            make.centerX.equalTo(self.snp_centerX)
        }
        
        problemBtn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(kStatusBarheight())
            make.width.height.equalTo(DIMENSION_VALUE(44))
            make.right.equalTo(self.snp_right)
        }
        
        headerImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(DIMENSION_VALUE(8.5) + kTopHeight)
            make.width.height.equalTo(DIMENSION_VALUE(64))
            make.left.equalTo(DIMENSION_VALUE(34))
        }
        
        nikeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headerImageView.snp.top)
            make.height.equalTo(DIMENSION_VALUE(29.5))
            make.left.equalTo(headerImageView.snp.right).offset(DIMENSION_VALUE(10.5))
            make.right.equalToSuperview().offset(DIMENSION_VALUE(-15))
        }
        
        IDLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(headerImageView.snp.bottom).offset(DIMENSION_VALUE(-11))
            make.height.equalTo(DIMENSION_VALUE(18))
            make.left.equalTo(headerImageView.snp.right).offset(DIMENSION_VALUE(10.5))
        }
        
        levelView.snp.makeConstraints { (make) in
            make.centerY.equalTo(IDLabel.snp.centerY)
            make.width.height.equalTo(DIMENSION_VALUE(18))
            make.left.equalTo(IDLabel.snp.right).offset(DIMENSION_VALUE(5))
        }
        
        lineView1.snp.makeConstraints { (make) in
            make.top.equalTo(headerImageView.snp.bottom).offset(DIMENSION_VALUE(21.5))
            make.height.equalTo(DIMENSION_VALUE(0.5))
            make.left.equalTo(snp.left).offset(DIMENSION_VALUE(15))
            make.right.equalTo(snp.right).offset(DIMENSION_VALUE(-15))
        }
        
    }
}
extension MineHeaderView{
    
    //MARK:-个人中心点击事件
    @objc func tabClick(_ tap:UITapGestureRecognizer){
        
        if !userManger.isLogined {
            return
        }
    
    }
    //MARK:-设置数据
    func setusermodel(model:UserInfo?) {
        guard model != nil else {
            self.nikeLabel.text = "游客"
            self.IDLabel.text = "登录/注册"
            return
        }
        self.headerImageView.kf.setImage(with: URL(string: model?.headimgurl ??  ""), placeholder: imageNamed("DefaultHeader"), options: nil, progressBlock: nil, completionHandler: nil)
        self.nikeLabel.text = model?.nickname
        self.IDLabel.text = "学号ID：\(model?.user_code ?? "")"
        
    }
  

}
